package cn.lhn.ridersclub.service;

import cn.lhn.ridersclub.dto.AccountCardDto;
import cn.lhn.ridersclub.dto.AccountDto;
import cn.lhn.ridersclub.entity.Account;
import cn.lhn.ridersclub.entity.AccountFollow;
import cn.lhn.ridersclub.entity.Grade;
import cn.lhn.ridersclub.mapper.AccountFollowMapper;
import cn.lhn.ridersclub.mapper.AccountMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountFollowServiceImpl implements AccountFollowService {

    private final AccountMapper accountMapper;

    private final AccountFollowMapper accountFollowMapper;

    private final GradeService gradeService;

    private final PaperService paperService;

    public AccountFollowServiceImpl(AccountMapper accountMapper, AccountFollowMapper accountFollowMapper, GradeService gradeService, PaperService paperService) {
        this.accountMapper = accountMapper;
        this.accountFollowMapper = accountFollowMapper;
        this.gradeService = gradeService;
        this.paperService = paperService;
    }

    @Override
    public void addFollow(Long accountId, Long fansId) {

        //  判断账号是否存在
        Account account = accountMapper.selectById(accountId);
        if (account == null) {
            throw new RuntimeException("账号不存在");
        }
        account = accountMapper.selectById(fansId);
        if (account == null) {
            throw new RuntimeException("账号不存在");
        }

        //  判断是否自己关注自己
        if (accountId.equals(fansId)) {
            throw new RuntimeException("不能关注自己");
        }

        //  查看是否已经关注
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", accountId);
        qw.eq("fans_id", fansId);
        AccountFollow follow = accountFollowMapper.selectOne(qw);
        if (follow != null) {
            throw new RuntimeException("请勿重复关注");
        }

        //  添加数据
        int i = accountFollowMapper.insert(new AccountFollow(0L, accountId, fansId));
        if (i == 0) {
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    public void deleteFollow(Long accountId, Long fansId) {
        //  删除关注记录
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", accountId);
        qw.eq("fans_id", fansId);
        int i = accountFollowMapper.delete(qw);
        if (i == 0) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public AccountFollow getFollow(Long accountId, Long fansId) {
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", accountId);
        qw.eq("fans_id", fansId);
        return accountFollowMapper.selectOne(qw);
    }

    @Override
    public List<AccountCardDto> getFollows(Long fansId, Long startId) {
        List<AccountFollow> followList = accountFollowMapper.listFollow(fansId, startId);
        List<AccountCardDto> list = new ArrayList<>();
        for (AccountFollow follow : followList) {
            AccountCardDto dto = toAccountCardDto(follow.getId(), accountMapper.selectById(follow.getAccountId()));
            list.add(dto);
        }
        return list;
    }

    @Override
    public List<AccountCardDto> getFans(Long accountId, Long startId) {
        List<AccountFollow> followList = accountFollowMapper.listFans(accountId, startId);
        List<AccountCardDto> list = new ArrayList<>();
        for (AccountFollow follow : followList) {
            list.add(toAccountCardDto(follow.getId(), accountMapper.selectById(follow.getFansId())));
        }
        return list;
    }

    @Override
    public Long getFollowCount(Long accountId) {
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("fans_id", accountId);
        return accountFollowMapper.selectCount(qw);
    }

    @Override
    public Long getFansCount(Long accountId) {
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", accountId);
        return accountFollowMapper.selectCount(qw);
    }

    @Override
    public boolean isFollow(Long id, Long accountId) {
        QueryWrapper<AccountFollow> qw = new QueryWrapper<>();
        qw.eq("account_id", id);
        qw.eq("fans_id", accountId);
        return accountFollowMapper.selectOne(qw) != null;
    }

    public AccountDto toAccountDto(Account account) {

        //  等级
        Grade grade = gradeService.getGrade(account.getId());
        Long maxGradeValue = gradeService.getMaxGradeValue(grade.getGrade());

        //  文章输数量
        Long paperCount = paperService.getPaperCount(account.getId());

        //  关注数和粉丝数
        Long followCount = getFollowCount(account.getId());
        Long fansCount = getFansCount(account.getId());

        return AccountDto
                .builder()
                .id(account.getId())
                .nickname(account.getNickname())
                .gender(account.getGender())
                .profile(account.getProfile())
                .birth(account.getBirth())
                .createTime(account.getCreateTime())
                .grade(grade.getGrade())
                .currentGradeValue(grade.getCurrentGradeValue())
                .maxGradeValue(maxGradeValue)
                .paperCount(paperCount)
                .followCount(followCount)
                .fansCount(fansCount)
                .build();
    }

    public AccountCardDto toAccountCardDto(Long id, Account account) {
        if (account == null) {
            return null;
        }

        return AccountCardDto
                .builder()
                .id(id)
                .accountId(account.getId())
                .nickname(account.getNickname())
                .profile(account.getProfile())
                .build();
    }
}
